package base.service.impl;


import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import base.dao.ClassinfoDao;
import base.dao.GradeDao;
import base.model.Classinfo;
import base.model.Grade;
import base.service.ClassinfoService;
import common.enums.ResultCodeEnum;
import common.exception.ServiceException;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;


/**
 * 
 * @author xiaozewen
 * @version  2016-11-26 11:02:29
 */
@Service
public class ClassinfoServiceImpl  extends BaseServiceImpl<Classinfo> implements ClassinfoService {
	@Autowired
    private ClassinfoDao classinfoDao;
	@Autowired
	private GradeDao gradeDao;
	
	@Override
	public int delete(String ids) {
		Example exampleGrade = new Example(Grade.class);
		exampleGrade.createCriteria().andIn("classId", Arrays.asList(ids.split(",")));
		List<Grade> grades = gradeDao.selectByExample(exampleGrade);
		if(grades.size()>0){
			throw new ServiceException(ResultCodeEnum.SERVICE_ERROR.getValue(), "课程已被当作成绩录入.不能删除");
		}
		Example example = new Example(Classinfo.class);
		example.createCriteria().andIn("id", Arrays.asList(ids.split(",")));
		return classinfoDao.deleteByExample(example);
	}

	@Override
	public List<Classinfo> list(Classinfo classinfo) {
		Example example=new Example(Classinfo.class);
		Criteria criteria = example.createCriteria();
		List<Classinfo> classinfos = classinfoDao.selectByExample(example);
		return classinfos;
	}

	@Override
	public PageInfo<Classinfo> listPage(Classinfo classinfo,Integer pageNum,Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Example example=new Example(Classinfo.class);
		Criteria criteria = example.createCriteria();
		if(StringUtils.isNotBlank(classinfo.getName())){
			criteria.andLike("name", "%%"+classinfo.getName()+"%%");
		}
		List<Classinfo> classinfos = classinfoDao.selectByExample(example);
		return new PageInfo<Classinfo>(classinfos);
	}

	@Override
	public List<Map<String, Object>> search(String name) {
		return classinfoDao.search(name);
	}

}
